<html>
<head><meta charset="utf-8"><title>mapping formatting args to references #76443 · t-compiler/wg-diagnostics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/index.html">t-compiler/wg-diagnostics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/mapping.20formatting.20args.20to.20references.20.2376443.html">mapping formatting args to references #76443</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210669299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/mapping%20formatting%20args%20to%20references%20%2376443/near/210669299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Macdonald <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/mapping.20formatting.20args.20to.20references.20.2376443.html#210669299">(Sep 20 2020 at 14:27)</a>:</h4>
<p>Hi. I'm working on this pull request <a href="https://github.com/rust-lang/rust/issues/76443">#76443</a> and in this file in particular: <code>compiler\rustc_builtin_macros\src\format.rs </code><br>
I could use some help from someone familiar with the code. <br>
I need to map each used formatting argument to the span of the formatting specifier referencing them.</p>
<p>Here's what I understand so far:<br>
<code>cx.arg_spans</code> has the span of all formatting specifiers found in the format string. <br>
You can get the position/ID (and spans) of used arguments using <code> cx.arg_types</code> this way:</p>
<div class="codehilite"><pre><span></span><code><span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">used_args</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cx</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">arg_types</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">enumerate</span><span class="p">()</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">_i</span><span class="p">,</span><span class="w"> </span><span class="n">ty</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="o">!</span><span class="n">ty</span><span class="p">.</span><span class="n">is_empty</span><span class="p">())</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="p">(</span><span class="n">cx</span><span class="p">.</span><span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">span</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">})</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;&gt;</span><span class="p">();</span><span class="w"></span>
</code></pre></div>


<p>Given the position/ID of these arguments, I imagined they would already be a mapping of format arguments to their references(e.g. arg_1 =&gt; [0, 3, 5], meaning arg_1 is reference by formatting specifier 0, 3, and 5).<br>
These fields in the Context struct seem to have related data but I don't quite understand what they represent:<br>
<code>arg_types </code><br>
<code>arg_unique_types</code><br>
<code>arg_index_map</code></p>
<p>Also, what does the<code>ArgumentType</code> enum represent ? I don't get what's the difference between <code>Placeholder</code> and <code>Count</code> variants.</p>
<p>If this mapping from formatting arguments to references is not stored in the Context struct, any idea where  I would look to get that data ?<br>
Thank you!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>